package com.zhike.service.user.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhike.common.dto.BlacklistAddDTO;
import com.zhike.common.model.Blacklist;
import com.zhike.common.vo.BlacklistVO;
import com.zhike.mapper.user.UserBlacklistMapper;
import com.zhike.service.user.IUserBlacklistService;

@Service
public class UserBlacklistServiceImpl extends ServiceImpl<UserBlacklistMapper, Blacklist>
        implements IUserBlacklistService {

    @Resource
    private UserBlacklistMapper userBlacklistMapper;

    @Override
    public Boolean checkInBlacklist(String uid, String toUid) {
        LambdaQueryWrapper<Blacklist> lqw = Wrappers.<Blacklist>lambdaQuery();
        lqw.eq(Blacklist::getFakeUid, uid);
        lqw.eq(Blacklist::getToUid, toUid);
        return count(lqw) > 0;
    }

    @Override
    public List<BlacklistVO> queryMyBlacklist(String fakeUid) {
        return userBlacklistMapper.queryMyBlacklist(fakeUid);
    }

    @Override
    public Boolean addBlacklist(String uid, BlacklistAddDTO dto) {
        Blacklist blacklist = new Blacklist();
        blacklist.setFakeUid(uid);
        blacklist.setToUid(dto.getToUid());
        blacklist.setPhone(dto.getPhone());
        blacklist.setName(dto.getNickname());
        blacklist.setIdCard(dto.getIdCard());
        blacklist.setCreateTime(new Date());
        return userBlacklistMapper.insert(blacklist) > 0;
    }

    @Override
    public Boolean cancelBlacklist(String uid, String toUid) {
        LambdaQueryWrapper<Blacklist> query = new LambdaQueryWrapper<>();
        query.eq(Blacklist::getFakeUid, uid);
        query.eq(Blacklist::getToUid, toUid);
        return userBlacklistMapper.delete(query) > 0;
    }

}
